package com.dong.service.User;

import com.dong.dao.BaseDao;
import com.dong.dao.User.UserDao;
import com.dong.dao.User.UserDaoImpl;
import com.dong.pojo.User;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;


public class UserServiceImpl implements UserService{
    //业务层都会调用Dao层，所以我们需要引入Dao层
    private UserDao userDao;
    public UserServiceImpl(){
        userDao = new UserDaoImpl();
    }

    public User Login(String userCode, String userPassword) {
        Connection connection = null;
        User user = null;

        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection,userCode);
            if(user == null){
                return user;
            }
            if(!userPassword.equals(user.getUserPassword())){
                return user = null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }

    //修改当前用户的密码
    public boolean updatePwd(int id, String userPassword) {
        Connection connection = null;
        boolean flag = false;

        connection = BaseDao.getConnection();
        try {
            if(userDao.updatePwd(connection, id, userPassword) > 0){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;

    }
    //查询记录数
    public int getUserCount(String username, int userRole) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection, username, userRole);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return count;

    }
    //通过条件去查询用户列表
    public List<User> getUserList(String queryUsername, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        System.out.println("queryUsername-->"+queryUsername);
        System.out.println("queryUserRole-->"+queryUserRole);
        System.out.println("currentPageNo-->"+currentPageNo);
        System.out.println("pageSize-->"+pageSize);

        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection,queryUsername,queryUserRole,currentPageNo,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return userList;
    }
    //通过用户id查找用户
    public User getUserByid(String id){
        Connection connection = null;
        User user = null;

        try {
            connection = BaseDao.getConnection();
            user = userDao.getUserByid(connection, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }

    //添加一新用户
    public boolean addUser(User user) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启jdbc事务管理
            int updateRows = userDao.addUser(connection, user);
            connection.commit();
            if(updateRows > 0 ){
                flag = true;
                System.out.println("adduser success!");
            }else{
                System.out.println("adduser failed!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                System.out.println("rollback==================");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    //根据userCode查询出User，是否存在userCode的人物
    public User selectUserCodeExist(String userCode) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, userCode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }

    //修改用户信息
    public boolean modifyUser(User user) {
        Connection connection = null;
        boolean flag = false;

        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务
            int modifyRows = userDao.modifyUser(connection, user);
            connection.commit();
            if(modifyRows > 0){
                flag = true;
                System.out.println("modify success!");
            }else{
                System.out.println("modify failed!");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                System.out.println("rollback==================");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    //删除用户个人信息
    public boolean deleteUserById(Integer delid) {
        Connection connection = null;
        boolean flag = false;

        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            int delRows = userDao.deleteUserById(connection, delid);
            connection.commit();
            if(delRows > 0){
                flag = true;
                System.out.println("deluser success!");
            }else{
                System.out.println("deluser failed!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                System.out.println("rollback==================");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    @Test
    public void Test(){
        UserServiceImpl userService = new UserServiceImpl();
        User user1 = userService.getUserByid("7");
        System.out.println(user1.getUserName());
    }


}

